package cn.tedu.springboot.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

//蓓蕾是用来测试JDBC删除的演示demo  将location表中id为33的记录删除
public class TestDelete {
    private static  Connection conn = null;
    private static  PreparedStatement ps = null;
    public static void main(String[] args) throws Exception {
        //1.加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        //2.获取和数据库的链接
        //2.1指定URL 确定要链接的是那个数据库
        String url = "jdbc:mysql://localhost:3306/tedu?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai";
        //2.2指定用户名和密码
        String user = "root";
        String psd = "root";
        //2.3调用DriverManager的getConnection方法获取链接
        conn = DriverManager.getConnection(url, user, psd);

        //3.定义sql语句
        String sql = "DELETE FROM location WHERE id =34";

        //4.获取执行sql语句的对象
        ps = conn.prepareStatement(sql);

        //5.通过PreparedStatement执行sql语句
        int rows = ps.executeUpdate();

        //6.处理结果
        System.out.println(rows > 0 ? "删除"+ rows + "行成功" : "删除失败");
        //7.释放资源
        ps.close();
        conn.close();
    }
}
